package Fisica;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Logica.Util;
import Modelo.CtaCte_Padron;


public class DAOPadron {
	public static String SELECT = "select * from ctacte_padron";	
	public static String FIRST = "SELECT * FROM ctacte_padron order by padron_codigo asc;";
	public static String LAST = "SELECT * FROM ctacte_padron order by padron_codigo desc;";
	public static String PRE = "SELECT * FROM ctacte_padron WHERE padron_codigo < ? order by padron_codigo desc;";
	public static String POST = "SELECT * FROM ctacte_padron WHERE padron_codigo > ? order by padron_codigo asc;";
		
	public static Modelo.CtaCte_Padron get (int ctacte_padron){
		String sql = SELECT + " where padron_codigo = ?;";
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(sql);	
			st.setInt(1, ctacte_padron);
			ResultSet rs = st.executeQuery();			
			
			if (rs.next()){
				Modelo.CtaCte_Padron a = new Modelo.CtaCte_Padron(); 
				a.setPadron_codigo(rs.getInt("padron_codigo"));
				a.setCodigo_postal(rs.getInt("codigo_postal"));
				a.setPadron_apelli(rs.getString("padron_apelli"));
				a.setPadron_nombre(rs.getString("padron_nombre")); 	
				a.setPadron_cuit11(rs.getString("padron_cuit11"));
				a.setPadron_domici(rs.getString("padron_domici"));
				a.setPadron_domno(rs.getString("padron_domnro"));
				a.setCodigo_postal(rs.getInt("codigo_postal"));
				a.setPadron_telcar(rs.getInt("padron_telcar"));
				a.setPadron_telnro(rs.getInt("padron_telnro"));
				a.setPadron_actanr(rs.getInt("padron_actanr"));
				a.setPadron_inges(Util.getDate(rs.getString("padron_ingres")));				
				
				return a;
			}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 				
		return new CtaCte_Padron();
	}

	
//	select * from ctacte_padron;
	public static List<Object> getList (){
		List<Object> list = new ArrayList();
		String sql = SELECT +";";;
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(sql);	
			ResultSet rs = st.executeQuery();			
			CtaCte_Padron a;
			while (rs.next()){				
				a = new Modelo.CtaCte_Padron(); 
				a.setPadron_codigo(rs.getInt("padron_codigo"));
				a.setCodigo_postal(rs.getInt("padron_postal"));
				a.setPadron_apelli(rs.getString("padron_apelli"));
				a.setPadron_nombre(rs.getString("padron_nombre")); 	
				a.setPadron_cuit11(rs.getString("padron_cuit11"));
				a.setPadron_domici(rs.getString("padron_domici"));
				a.setPadron_domno(rs.getString("padron_domnro"));
				a.setCodigo_postal(rs.getInt("codigo_postal"));
				a.setPadron_telcar(rs.getInt("padron_telcar"));
				a.setPadron_telnro(rs.getInt("padron_telnro"));
				a.setPadron_actanr(rs.getInt("padron_actanr"));
				a.setPadron_inges(Util.getDate(rs.getString("padron_ingres")));	
				
				list.add(a);
			}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 				
		return list;
	}
	
	public static void main(String[] args) {
		DAOPadron p = new DAOPadron();
//		p.getArticulo();
		
		CtaCte_Padron t = new CtaCte_Padron();
//		t.setPadron_codigo(9999);
//		t.setNumero("99999999");
//		t.setExtencion("0");
//		t.setFoto("\\\\192.168.1.4\\carpeta con espacios\\archivo con espacios.pdf");  //lo ingresa correctamente.
//		t.setAlta(new Date());
//		t.setPadron_codigoCategoria(9);
//		t.setPadron_codigoRelacion(9);
		
//		p.insert(t);
		
		t = p.get(2);
		
		

	}
	
	public Object getObject (PreparedStatement st){ //ok
		CtaCte_Padron a = new CtaCte_Padron(); 	
		
		try {
//			PreparedStatement st = Conexion.getConexion().prepareStatement(sql);	
			ResultSet rs = st.executeQuery();			
			FileOutputStream archivofoto;
			File file; 
			if (rs.next()){
				a.setPadron_codigo(rs.getInt("padron_codigo"));
				a.setCodigo_postal(rs.getInt("padron_postal"));
				a.setPadron_apelli(rs.getString("padron_apelli"));
				a.setPadron_nombre(rs.getString("padron_nombre")); 	
				a.setPadron_cuit11(rs.getString("padron_cuit11"));
				a.setPadron_domici(rs.getString("padron_domici"));
				a.setPadron_domno(rs.getString("padron_domnro"));
				a.setCodigo_postal(rs.getInt("codigo_postal"));
				a.setPadron_telcar(rs.getInt("padron_telcar"));
				a.setPadron_telnro(rs.getInt("padron_telnro"));
				a.setPadron_actanr(rs.getInt("padron_actanr"));
				a.setPadron_inges(Util.getDate(rs.getString("padron_ingres")));	
			}	
			return a;
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 		
		return new CtaCte_Padron();		
	}

	public Object getPre(int padron_codigo) {
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(PRE);
			st.setInt(1, padron_codigo);
			return getObject(st);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return null;
	}

	public Object getPost(int padron_codigo) {
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(POST);
			st.setInt(1, padron_codigo);
			return getObject(st);
		} catch (SQLException e) {			
			e.printStackTrace();
		}		
		return null;
	}
	
	public Object getFirst() {
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(FIRST);
			return getObject(st);
		} catch (SQLException e) {			
			e.printStackTrace();
		}		
		return null;
	}
	
	public Object getLast() {
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(LAST);
			return getObject(st);			
		} catch (SQLException e) {			
			e.printStackTrace();
		}		
		return null;
	}

}
